package com.jinzhi.jzweb.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzapi.common.controller.ApittController;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.pojo.ExaminationAnswerSheetComExlVO;
import com.jinzhi.jzweb.pojo.ExaminationAnswerSheetComVO;
import com.jinzhi.jzweb.pojo.StuSignUpComVo;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.jzweb.utils.DoubleUtil;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import com.jinzhi.jzweb.utils.ImgMp4ValueUtil;
import com.jinzhi.sys.service.UserRoleService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * 超级管理员 总裁篇查看分数
 * <pre>
 * A赛场查看分数 统计
 * 2023
 * </pre>
 * <small> 2023-05-05 15:11:09 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/examinationAnswerSheetComSour")
public class ExaminationAnswerSheetComSourController extends BaseController {
    @Autowired
    private ExaminationAnswerSheetComService examinationAnswerSheetComService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private ExaminationAnswerSheetComService examinationAnswerSheetService;
    @Autowired
    private ExaminationPaperService examinationPaperService;
    @Autowired
    private StuSignUpComService stuSignUpService;
    @Autowired
    private ExaminationQuestionsService examinationQuestionsService;
    @Autowired
    private ExaminationBatchService examinationBatchService;
    @Autowired
    private ExaminationResultsService resultsService;
    @Autowired
    private ExaminationResultsComService examinationResultsService;
    @Autowired
    private ApittController apittController;
    @Autowired
    private  ExaminationResultsImgMp4ComService imgMp4ComService;
    /**
     * 超级管理员 一级管理员查看分数
     * @param model
     * @return
     */
    @GetMapping()
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:examinationAnswerSheetComSour")
    String info( Model model) {
        return "jzweb/examinationAnswerSheetCom/examinationAnswerSheetComSour";
    }

    /**
     * 跳转
     * 超级管理员修改理论分数
     * 理论修改分数跳转
     * @return
     */
    @GetMapping("/exitScore")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:add")
    String exitScore(){
        return "jzweb/examinationAnswerSheetCom/exitScore";
    }

    /**
     *A赛场成绩导出
     *
     * @param response
     */
    @GetMapping("/downloadImportStu")
    @RequiresPermissions("jzweb:stuSignUpCom:import")
    @ResponseBody
    public void downloadImportTemplate(HttpServletResponse response, ExaminationResultsComDO examinationAnswerSheetComDT) {
        List<ExaminationAnswerSheetComExlVO> list1 = new LinkedList<>();
        Wrapper<StuSignUpComDO> wrapper=new EntityWrapper<>();

        if(StringUtils.isNotBlank(examinationAnswerSheetComDT.getUnitName())){
            wrapper.like("unit_name",examinationAnswerSheetComDT.getUnitName());
        }
        // 查询列表数据
        Page<StuSignUpComDO> list = stuSignUpService.selectPage(getPage(StuSignUpComDO.class),
                wrapper.orderBy("id", false));
        for(StuSignUpComDO stuSignUpComDO:list.getRecords()){
            ExaminationResultsComDO examinationResultsComDO=  examinationResultsService.findOneByKv("stu_sign_id",stuSignUpComDO.getId());
            if(examinationResultsComDO!=null){
                ExaminationResultsComDO obj = examinationResultsComDO;
                ExaminationAnswerSheetComExlVO exlVO=new ExaminationAnswerSheetComExlVO();
                exlVO.setNum2(stuSignUpComDO.getNum2());
                exlVO.setName(stuSignUpComDO.getName());
                exlVO.setUnitName(stuSignUpComDO.getUnitName());
                exlVO.setIdNumber(stuSignUpComDO.getIdNumber());
              //  exlVO.setTicketNumber(stuSignUpComDO.getTicketNumber());
                Double sour=obj.getTotalScore();
                if(obj.getScore1()==null){
                    obj.setScore1(0.0);
                }
                if(obj.getScore2()==null){
                    obj.setScore2(0.0);
                }
                if(obj.getScore3()==null){
                    obj.setScore3(0.0);
                }
                Double f=(sour*0.3);

                BigDecimal bg = new BigDecimal(f);
              //  exlVO.setScore1(DoubleUtil.douToTwo(obj.getScore1()*0.15));
              //  exlVO.setScore2(DoubleUtil.douToTwo(obj.getScore2()*0.15));
              //  exlVO.setScore3(DoubleUtil.douToTwo(obj.getScore3()*0.15));
                double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

                exlVO.setScore(f1);
                list1.add(exlVO);
            }else {
              //  ExaminationResultsComDO obj = new ExaminationResultsComDO();
                ExaminationAnswerSheetComExlVO obj=new ExaminationAnswerSheetComExlVO();
                obj.setNum2(stuSignUpComDO.getNum2());
                obj.setName(stuSignUpComDO.getName());
                obj.setUnitName(stuSignUpComDO.getUnitName());
                obj.setIdNumber(stuSignUpComDO.getIdNumber());
               // obj.setScore1(0.0);
              //  obj.setScore2(0.0);
              //  obj.setScore3(0.0);
                obj.setScore(0.0);
                list1.add(obj);

            }
           // list1.add(stuSignUpComVo);
        }

        //导出操作
        EasyPoiUtil.exportExcel(list1, "A赛场成绩导出", "A赛场成绩导出", ExaminationAnswerSheetComExlVO.class, "A赛场成绩导出", response);
    }

    /**
     * A赛场理论成绩统计
     * @param examinationAnswerSheetComDTO
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:examinationAnswerSheetComSour")
    public Result<Page<StuSignUpComDO>> list(ExaminationResultsComDO examinationAnswerSheetComDTO){
        Wrapper<StuSignUpComDO> wrapper=new EntityWrapper<>();

        if(StringUtils.isNotBlank(examinationAnswerSheetComDTO.getUnitName())){
            wrapper.like("unit_name",examinationAnswerSheetComDTO.getUnitName());
        }

        // 查询列表数据
        Page<StuSignUpComDO> page = stuSignUpService.selectPage(getPage(StuSignUpComDO.class),
                wrapper.orderBy("id", false));
        List list = new ArrayList();
        for (int i = 0; i < page.getRecords().size(); i++) {
            ExaminationResultsComDO examinationResultsComDO=  examinationResultsService.findOneByKv("stu_sign_id",page.getRecords().get(i).getId());
            StuSignUpComDO stuSignUpComDO=page.getRecords().get(i);
            if(examinationResultsComDO!=null){
                ExaminationResultsComDO obj = new ExaminationResultsComDO();
                ExaminationResultsComResourcesDO img= imgMp4ComService.findOneByKv("results_id",examinationResultsComDO.getId());
                if(img!=null){
                    obj= ImgMp4ValueUtil.testRear(examinationResultsComDO,img);
                }else {
                    obj = examinationResultsComDO;
                }
                obj.setNum2(stuSignUpComDO.getNum2());
                obj.setName(stuSignUpComDO.getName());
                obj.setUnitName(stuSignUpComDO.getUnitName());
                obj.setIdNumber(stuSignUpComDO.getIdNumber());
                obj.setTicketNumber(stuSignUpComDO.getTicketNumber());
                Double sour=obj.getTotalScore();
                if(obj.getScore1()==null){
                    obj.setScore1(0.0);
                }
                if(obj.getScore2()==null){
                    obj.setScore2(0.0);
                }
                if(obj.getScore3()==null){
                    obj.setScore3(0.0);
                }
                Double f=(sour*0.3);

                BigDecimal bg = new BigDecimal(f);
                obj.setScore1(DoubleUtil.douToTwo(obj.getScore1()*0.15));
                obj.setScore2(DoubleUtil.douToTwo(obj.getScore2()*0.15));
                obj.setScore3(DoubleUtil.douToTwo(obj.getScore3()*0.15));
                double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

                obj.setScore(f1);
                list.add(obj);
            }else {
                ExaminationResultsComDO obj = new ExaminationResultsComDO();
                obj.setNum2(stuSignUpComDO.getNum2());
                obj.setName(stuSignUpComDO.getName());
                obj.setUnitName(stuSignUpComDO.getUnitName());
                obj.setIdNumber(stuSignUpComDO.getIdNumber());
                obj.setTicketNumber(stuSignUpComDO.getTicketNumber());
                Double sour=obj.getTotalScore();
               obj.setScore1(0.0);
               obj.setScore2(0.0);
               obj.setScore3(0.0);
                list.add(obj);

            }
        }
//        // 查询列表数据
//        Page<ExaminationResultsComDO> page = examinationResultsService.selectPage(getPage(ExaminationResultsComDO.class),
//                examinationResultsService.convertToEntityWrapper().orderBy("id", false));
//        List list = new ArrayList();
//        for (int i = 0; i < page.getRecords().size(); i++) {
//            ExaminationResultsComDO obj = (ExaminationResultsComDO) page.getRecords().get(i);
//          StuSignUpComDO stuSignUpComDO=  stuSignUpService.selectById(obj.getStuSignId());
//          if(stuSignUpComDO!=null){
//            obj.setNum2(stuSignUpComDO.getNum2());
//            obj.setName(stuSignUpComDO.getName());
//            obj.setUnitName(stuSignUpComDO.getUnitName());
//            obj.setIdNumber(stuSignUpComDO.getIdNumber());
//            obj.setTicketNumber(stuSignUpComDO.getTicketNumber());
//            Double sour=obj.getTotalScore();
//            if(obj.getScore1()==null){
//                obj.setScore1(0.0);
//            }
//            if(obj.getScore2()==null){
//                obj.setScore2(0.0);
//            }
//            if(obj.getScore3()==null){
//                obj.setScore3(0.0);
//            }
//            Double f=sour+((obj.getScore1()+obj.getScore2()+obj.getScore3())/3);
//            BigDecimal bg = new BigDecimal(f);
//            double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//            obj.setScore(f1);
//            list.add(obj);
//          }
//        }

        page.setRecords(list);
        return Result.ok(page);
    }





    @GetMapping("/edit/{id}")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:edit")
    String edit(@PathVariable("id") Long id,Model model) {

        ExaminationResultsDO examinationResultsDO= resultsService.selectById(id);
        model.addAttribute("examinationResultsDO", examinationResultsDO);
//        ExaminationAnswerSheetComDO examinationAnswerSheetCom = examinationAnswerSheetComService.selectById(id);
//        ExaminationAnswerSheetComVO answerSheetComVO = new ExaminationAnswerSheetComVO();
//        String vauel = examinationAnswerSheetCom.getAnswerList();
//        JSONArray jsonArray = JSONObject.parseArray(vauel);
//        for (int i = 0; i < jsonArray.size(); i++) {
//            JSONObject jsonObj = jsonArray.getJSONObject(i);
//            String type = jsonObj.getString("type");
//            if (type.equals("4")) {
//                StuSignUpComDO stuSignUpComDO = stuSignUpService.selectById(examinationAnswerSheetCom.getStuSignId());
//                if (stuSignUpComDO != null) {
//                    answerSheetComVO.setAnswerList(jsonObj.getString("value"));
//                    answerSheetComVO.setState(stuSignUpComDO.getState().toString());
//                    answerSheetComVO.setNum2(stuSignUpComDO.getNum2());
//                    answerSheetComVO.setCourt(stuSignUpComDO.getCourt());
//                    answerSheetComVO.setId(stuSignUpComDO.getId());
//                    answerSheetComVO.setExamBatchId(stuSignUpComDO.getCbatchId());
//                }
//                model.addAttribute("answerSheetComVO", answerSheetComVO);
//            }
//        }
        return "jzweb/examinationAnswerSheetCom/edit";
    }

    /**
     * 修改分数
     * 超级管理员修改理论分数
     * @param examinationAnswerSheetCom
     * @return
     */
    @Log("修改考试答卷")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("jzweb:examinationResultsABC:edit")
    public Result<String>  update( ExaminationResultsComDO examinationAnswerSheetCom){
//        if(examinationAnswerSheetCom.getScore1()!=null){
//            examinationAnswerSheetCom.setScore1(examinationAnswerSheetCom.getScore1());
//        }else if(examinationAnswerSheetCom.getType()==2){
//            examinationAnswerSheetCom.setScore2(examinationAnswerSheetCom.getScore1());
//        }else if(examinationAnswerSheetCom.getType()==3){
//            examinationAnswerSheetCom.setScore3(examinationAnswerSheetCom.getScore1());
//        }


        boolean update = examinationResultsService.updateById(examinationAnswerSheetCom);
        return update ? Result.ok() : Result.fail();
    }



    @Log("添加考试答卷")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:add")
    public Result<String> save( ExaminationAnswerSheetComDO examinationAnswerSheetCom){
        examinationAnswerSheetComService.insert(examinationAnswerSheetCom);
        return Result.ok();
    }



    @Log("删除考试答卷")
    @PostMapping( "/remove")
    @ResponseBody
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:remove")
    public Result<String>  remove( Long id){
        examinationAnswerSheetComService.deleteById(id);
        return Result.ok();
    }

    @Log("批量删除考试答卷")
    @PostMapping( "/batchRemove")
    @ResponseBody
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:batchRemove")
    public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
        examinationAnswerSheetComService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }

}
